package com.car.loan.project.cm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.car.loan.common.enums.SwitchStatus;
import com.car.loan.common.enums.UserStatus;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.project.cm.domain.CmBank;
import com.car.loan.project.cm.util.select.BankBaseDataSelect;
import com.car.loan.project.cm.util.select.BankCarTypeBaseDataSelect;
import com.car.loan.project.cm.util.vo.SelectedVo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.loan.project.cm.mapper.CmBankCarTypeMapper;
import com.car.loan.project.cm.domain.CmBankCarType;
import com.car.loan.project.cm.service.ICmBankCarTypeService;

/**
 * 银行车辆类型Service业务层处理
 *
 * @author ruoyi
 * @date 2019-11-01
 */
@Service
public class CmBankCarTypeServiceImpl implements ICmBankCarTypeService {

    @Autowired
    private CmBankCarTypeMapper cmBankCarTypeMapper;

    /**
     * 查询银行车辆类型
     *
     * @param id 银行车辆类型ID
     * @return 银行车辆类型
     */
    @Override
    public CmBankCarType selectCmBankCarTypeById(Long id) {
        return cmBankCarTypeMapper.selectById(id);
    }

    /**
     * 查询银行车辆类型列表
     *
     * @param cmBankCarType 银行车辆类型
     * @return 银行车辆类型
     */
    @Override
    public List<CmBankCarType> selectCmBankCarTypeList(CmBankCarType cmBankCarType) {
        QueryWrapper<CmBankCarType> queryWrapper = new QueryWrapper<>(cmBankCarType);
        return cmBankCarTypeMapper.selectList(queryWrapper);
    }


    @Override
    public IPage<CmBankCarType> selectPage(IPage<CmBankCarType> page,
        QueryWrapper<CmBankCarType> queryWrapper) {
        return cmBankCarTypeMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SelectedVo> selectedList(BankCarTypeBaseDataSelect bankBaseDataSelect) {
        CmBankCarType cmBankCarType = new CmBankCarType();
        cmBankCarType.setBankId(bankBaseDataSelect.getBankId());
        QueryWrapper<CmBankCarType> queryWrapper = new QueryWrapper<>(cmBankCarType);

        if (null == bankBaseDataSelect.getOf() || SwitchStatus.OPEN.status == bankBaseDataSelect
            .getOf()) {
            queryWrapper.eq("status", UserStatus.OK.getCode());
        } else {
            queryWrapper.in("status", UserStatus.OK.getCode(), UserStatus.DISABLE.getCode());
        }
        queryWrapper.orderByDesc("id");
        List<CmBankCarType> cmBanks = cmBankCarTypeMapper.selectList(queryWrapper);
        List<SelectedVo> selectedVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(cmBanks)) {
            cmBanks.forEach(cmDealer -> {
                selectedVos.add(new SelectedVo().setValue(String.valueOf(cmDealer.getId()))
                    .setLabel(cmDealer.getCarType()).setStatus(cmDealer.getStatus()));
            });
        }
        return selectedVos;
    }

    /**
     * 新增银行车辆类型
     *
     * @param cmBankCarType 银行车辆类型
     * @return 结果
     */
    @Override
    public int insertCmBankCarType(CmBankCarType cmBankCarType) {

        return cmBankCarTypeMapper.insert(cmBankCarType);
    }

    /**
     * 修改银行车辆类型
     *
     * @param cmBankCarType 银行车辆类型
     * @return 结果
     */
    @Override
    public int updateCmBankCarType(CmBankCarType cmBankCarType) {

        return cmBankCarTypeMapper.updateById(cmBankCarType);
    }

    /**
     * 删除银行车辆类型对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmBankCarTypeByIds(Collection<Long> ids) {
        return cmBankCarTypeMapper.deleteBatchIds(ids);
    }

    /**
     * 删除银行车辆类型信息
     *
     * @param id 银行车辆类型ID
     * @return 结果
     */
    @Override
    public int deleteCmBankCarTypeById(Long id) {
        return cmBankCarTypeMapper.deleteById(id);
    }
}
